<?php
/**
 * 后台默认控制器
 * AttachmentController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class AttachmentbakController extends AdminController
{
	public function accessRules()
	{
		return array(
				array('allow',  // 所有人都允许访问的方法
						'actions'=>array('index'),
						'users'=>array('*'),
				),
				array('allow', // 允许登录用户访问的方法
						'actions'=>array('delete','upload','avatar','upfile','screenshot','crop','uploaddaren','uploadheader','uploadpetheader','uploadpetcustom' ,'adminUploadPic' ,'adminDeletePic' ,'adminUploadHeader' ,'adminDeleteHeader'),
						'users'=>array('@'),
				),
				array('deny',
						'users'=>array('*'),
				),
		);
	}
    public function actionIndex(){
        $this->render('index');
    }
    //后台上传照片
    public function actionUpload( ){
        $uid = Yii::app()->user->id;
       if( empty($uid) ){
            //$this->render('upfile');exit;
           echo json_encode(array('error'=>0,'msg'=>'用户ID错误'));
           exit;
       }
       $images = isset($_FILES['Filedata']) ? $_FILES['Filedata'] : '';         
       $aResult = array();       
       if( $images  ){
            list($iWidth,$iHeight)=getimagesize($images['tmp_name']);
			
            $postfix='jpg';
			if( !in_array($postfix ,array('jpg' ,'jpeg')) ){
                echo json_encode(array('error'=>0,'msg'=>'对不起目前照片格式只支持*.jpg *.jpeg'));
                exit;
			}
            if( $iWidth < 1080 || $iHeight < 720 ){
                //$aResult['err'] = 0;
                //$aResult['msg'] = '对不起，你上传的图片尺寸太小，请上传大于1080*720像素的图片';
                //echo json_encode($aResult);exit;
			}            
            //创建上传目录
            $saveLocalPath=Yii::app()->params['upload']['uploadlocal'];
            $savePath = 'albumpic' . '/' . $uid ;
            $asavePath = explode('/' ,$savePath );
            foreach ($asavePath as $v){
                $saveLocalPath.=$v.'/';
                if(!file_exists($saveLocalPath)){
                    @mkdir($saveLocalPath);
                    @chmod($saveLocalPath ,0777);
                }
            }            
			$postfix='jpg';
			$filename = rand(10000,99999) . date('YmdHis').'.'.$postfix;		
	       //echo $saveLocalPath . $filename;exit;
			if(move_uploaded_file($images['tmp_name'] ,$saveLocalPath . $filename)){
        		$return = array(
                    'status' => 1,
                    'type' => 'image',
                    'name' => $_FILES['Filedata']['name'],
                    'url' => '/'.$saveLocalPath  .$filename ,
                    'id'=>10
                ); 
               echo json_encode($return);
               exit;			
			}else{
				echo json_encode(array('status'=>0,'type'=>'image','msg'=>'上传失败！'));exit;
			};
            
       } else {
           echo json_encode(array('status'=>0,'type'=>'image','msg'=>'未选择上传文件'));
           exit;
       }
    }
    //删除上传文件
    public function actionDelete($aid){
        $Attachment=Attachment::model()->findByPk($aid,'uid=:uid',array(':uid'=>isset(Yii::app()->user->id)?Yii::app()->user->id:0));
        if(!empty($Attachment) && $Attachment->delete()){
            die(json_encode(array('err'=>0,'msg'=>'删除成功')));
        }else{
            die(json_encode(array('err'=>1,'msg'=>'删除失败')));
        }
    }
	//上传图片处理
	public function actionUpload__(){
		//检查是否指定上传的模块
		if(!isset($_POST['modtype'])){
			die(json_encode(array('err'=>0,'msg'=>'上传位置设置错误')));
		}
		//远文件名
		$attributes['oldName']=$_FILES['filedata']['name'];
	    //后缀
	    $attributes['extensionName']=Yii::app()->common->getFilePostfix($_FILES['filedata']['name']);
	    //大小
	    $attributes['filesize']=$_FILES['filedata']['size'];
	    //相对路径(创建文件夹和文件名称)
	    $attributes['serverpath']=Yii::app()->common->getPathFileName($attributes['extensionName']);
	    //文件名称
	    $attributes['filename']=basename($attributes['serverpath']);
	    //文件路径
	    $attributes['filepath']=Yii::app()->params['upload']['uploadview'].$attributes['serverpath'];
	    //判断文件类型是否在允许的范围内
	    if(!in_array($attributes['extensionName'],(array)Yii::app()->params['upload']['allowtype'])){
	        die(json_encode(array('err'=>0,'msg'=>'对不起！你上传的文件不再允许的范围内！')));
	    }
	    
	    //判断是通过本地还是ftp上传
	    if(Yii::app()->params['upload']['ftpenable'])
	    {//开启ftp上传
	        $ftp = Yii::app()->ftp;
	        $ftp->chdir('/');
	        $result=$ftp->put($attributes['serverpath'],$_FILES['filedata']['tmp_name'],FTP_BINARY);
	        $attributes['location']='ftp';
	    }else
	    {//本地上传
	        $uploadlocal=Yii::app()->params['upload']['uploadlocal'].$attributes['serverpath'];
	        $result=@copy($_FILES['filedata']['tmp_name'],$uploadlocal);
	        Yii::app()->common->checkImageWH($uploadlocal);
	        $attributes['location']='0';
	    }
	    @unlink($_FILES['filedata']['tmp_name']);
	    
	    if(!$result){
	        die(json_encode(array('err'=>0,'msg'=>'文件保存失败，请联系服务器管理员！')));
	    }
	    
	    $attributes['modtype']=isset($_POST['modtype'])?trim($_POST['modtype']):'';
	    
	    
	    $return=Yii::app()->common->saveFile($attributes);
	    echo json_encode($return);exit;
	}
}